using System.Collections.Generic;

namespace Weever.Json.Serialization.v1
{
    public class CycleReferenceChecker
    {
        private Stack<object> _referencesStack = new Stack<object>();

        public bool Push(object obj)
        {
            foreach (object o in _referencesStack)
            {
                if (ReferenceEquals(o, obj))
                    return false;
            }
            _referencesStack.Push(obj);
            return true;
        }

        public void Pop(object obj)
        {
            for (int i = _referencesStack.Count; i > 0; i--)
                if (ReferenceEquals(obj, _referencesStack.Pop()))
                    return;
        }

        internal int Count
        {
            get { return _referencesStack.Count; }
        }
    }
}
